﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_Accounts_SaleRegister : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    DataTable DTMain = new DataTable();
    AccountSettings acntstng = new AccountSettings();
    string previousTransaction = "";
    int firstRow = 1;
    Double totaldebit = 0;
    Double totalcredit = 0;
    int sno = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {

            string finyear = Session["finyear"].ToString();
            hidFinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindCustomData(txtFromDate.Text, txtToDate.Text);            
        }
    }

    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindCustomData(txtFromDate.Text, txtToDate.Text);
    }
    protected void BindCustomData(string strFromDate, string strToDate)
    {
        DataTable DTMain = new DataTable();
        if (strFromDate != string.Empty && strToDate != string.Empty)
        {
            hidFromDate.Value = strFromDate;
            hidToDate.Value = strToDate;
            hidShowOpt.Value = rdbtnlst1.SelectedValue.ToString();
            SqlParameter[] sqlpara = {
                                   new SqlParameter("@FromDate",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(strFromDate))),
                                   new SqlParameter("@ToDate",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(strToDate))),
                                   new SqlParameter("@TransactionTypeList","10,11")
                               };
            DTMain = DU.getdatausingprocedure("USP_ACC_AccountBookVoucherDetails", sqlpara);
            lblMsg.Text = "Sales Register From " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strFromDate)) + " To " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strToDate)) + ".";
        }
        DataView DVMain = DTMain.DefaultView;
        if (DTMain.Rows.Count > 0)
        {
            //DVMain.RowFilter = "ReferenceAmount<>0";            
            DVMain.RowFilter = "ReferenceAmount<>0";
        }
        DataTable dt = DVMain.ToTable();
        DataView dv =dt.DefaultView;
        if (rdbtnlst1.SelectedValue == "0")
        {
            dv.RowFilter = "Amount<0";
        }
        else if(rdbtnlst1.SelectedValue == "1")
        {
            dv.RowFilter = "Amount>0";
        }
     
        grdDisplay.DataSource = dv;
        grdDisplay.DataBind();
    }

    protected void grdDisplay_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        BindCustomData(txtFromDate.Text, txtToDate.Text);
    }

    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdDisplay = (GridView)sender;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblsno = (Label)e.Row.FindControl("lblsno");
            lblsno.Text = sno.ToString();
            sno++;
            Label lbldebit = (Label)e.Row.FindControl("lblDebit");
            Label lblCredit = (Label)e.Row.FindControl("lblCredit");
            int ParentTranType = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ParentTransactionType"));
            Double debit = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Debit"));
            Double credit = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Credit"));
            totalcredit = totalcredit + Math.Abs(credit);
            totaldebit = totaldebit + Math.Abs(debit);

            if (Math.Abs(credit) > 0 && ParentTranType == 11)
            {
                lblCredit.Text = acntstng.GetCurrency(Convert.ToDouble(Math.Abs(credit)));
            }
            if (Math.Abs(debit) > 0 && ParentTranType == 10)
            {
                lbldebit.Text = acntstng.GetCurrency(Convert.ToDouble(Math.Abs(debit)));
            }

            //We're only interested in Rows that contain data
            //get a reference to the data used to databound the row
            DataRowView drv = ((DataRowView)e.Row.DataItem);
            if (previousTransaction == drv["transactionID"].ToString())
            {
                sno--;
                //If it's the same category as the previous one
                //Increment the rowspan
                if (grdDisplay.Rows[firstRow].Cells[2].RowSpan == 0)
                {
                    grdDisplay.Rows[firstRow].Cells[2].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstRow].Cells[2].RowSpan += 1;
                }

                if (grdDisplay.Rows[firstRow].Cells[1].RowSpan == 0)
                {
                    grdDisplay.Rows[firstRow].Cells[1].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstRow].Cells[1].RowSpan += 1;
                }

                if (grdDisplay.Rows[firstRow].Cells[0].RowSpan == 0)
                {
                    grdDisplay.Rows[firstRow].Cells[0].RowSpan = 2;
                }
                else
                {
                    grdDisplay.Rows[firstRow].Cells[0].RowSpan += 1;
                }
                //Remove the cell
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
                e.Row.Cells.RemoveAt(0);
            }
            else //It's a new category
            {
                //Set the vertical alignment to top
                e.Row.VerticalAlign = VerticalAlign.Middle;
                //Maintain the category in memory
                previousTransaction = drv["transactionID"].ToString();
                firstRow = e.Row.RowIndex;
            }         
           
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "Total";
            e.Row.Cells[5].Text = acntstng.GetCurrency(totaldebit);
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalcredit);
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
        }
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        BindCustomData(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }

   
    //public static void PrintWebControl(Control ctrl)
    //{
    //    StringWriter stringWrite = new StringWriter();
    //    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    //    if (ctrl is WebControl)
    //    {
    //        Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
    //    }
    //    Page pg = new Page();
    //    pg.EnableEventValidation = false;
    //    pg.ClientScript.RegisterStartupScript(pg.GetType(), "Print_Sale_Transaction(s)", "window.open('about:blank','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
       
    //    HtmlForm frm = new HtmlForm();
    //    pg.Controls.Add(frm);
    //    frm.Attributes.Add("runat", "server");
    //    frm.Controls.Add(ctrl);
    //    pg.DesignerInitialize();
    //    pg.RenderControl(htmlWrite);
    //    string strHTML = stringWrite.ToString();
    //    HttpContext.Current.Response.Clear();
    //    HttpContext.Current.Response.Write(strHTML);
    //    HttpContext.Current.Response.Write("<script>window.print();</script>");
    //    HttpContext.Current.Response.End();
    //}


    protected void rdbtnlst1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCustomData(txtFromDate.Text, txtToDate.Text);
    }
}
